/*
 * DirectoryListSet.java - Directory list matcher
 * :tabSize=8:indentSize=8:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 1999, 2000, 2001 Slava Pestov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package erlangtools;

//{{{ Imports
import java.awt.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.SwingUtilities;

import org.gjt.sp.jedit.MiscUtilities;
import org.gjt.sp.jedit.jEdit;
import org.gjt.sp.jedit.io.VFS;
import org.gjt.sp.jedit.io.VFSManager;
import org.gjt.sp.jedit.search.BufferListSet;
import org.gjt.sp.util.Log;

/**
 * Recursive search over many directories.
 * @author Luis Chiruzzo
 * @version $Id: ManyDirectoriesListSet.java 5427 2008-03-31 $
 */
public class ManyDirectoriesListSet extends BufferListSet
{
	//{{{ DirectoryListSet constructor
	public ManyDirectoriesListSet(List<String> dirs, String glob, boolean recurse)
	{
		this.dirs = dirs;
		this.glob = glob;
		this.recurse = recurse;
		this.skipBinary = jEdit.getBooleanProperty("search.skipBinary.toggle");
		this.skipHidden = jEdit.getBooleanProperty("search.skipHidden.toggle");
	} //}}}



	//{{{ getFileFilter() method
	public String getFileFilter() {
		return glob;
	} //}}}

	//{{{ setFileFilter() method
	/**
	 * @since jEdit 4.2pre1
	 */
	public void setFileFilter(String glob) {
		this.glob = glob;
		invalidateCachedList();
	} //}}}

	//{{{ isRecursive() method
	public boolean isRecursive() {
		return recurse;
	} //}}}

	//{{{ setRecursive() method
	/**
	 * @since jEdit 4.2pre1
	 */
	public void setRecursive(boolean recurse) {
		this.recurse = recurse;
		invalidateCachedList();
	} //}}}

	//{{{ getCode() method
	public String getCode() {
		return "new ManyDirectoriesListSet(\"" + MiscUtilities.charsToEscapes(dirs.toString())
			+ "\",\"" + MiscUtilities.charsToEscapes(glob) + "\","
			+ recurse + ")";
	} //}}}

	private String[] getForOneDir(final String directory, final Component comp) {
		final VFS vfs = VFSManager.getVFSForPath(directory);
		Object session;
		if(SwingUtilities.isEventDispatchThread()) {
			session = vfs.createVFSSession(directory,comp);
		} else {
			final Object[] returnValue = new Object[1];

			try {
				SwingUtilities.invokeAndWait(new Runnable()
				{
					public void run()
					{
						returnValue[0] = vfs.createVFSSession(directory,comp);
					}
				});
			} catch(Exception e) {
				Log.log(Log.ERROR,this,e);
			}

			session = returnValue[0];
		}

		if(session == null) return null;
		try {
			return vfs._listDirectory(session,directory,glob,recurse,comp, skipBinary, skipHidden);
		}
		catch(IOException io)
		{
			VFSManager.error(comp,directory,"ioerror",new String[]
				{ io.toString() });
			return null;
		}
	}
	
	//{{{ _getFiles() method
	protected String[] _getFiles(final Component comp)
	{
		this.skipBinary = jEdit.getBooleanProperty("search.skipBinary.toggle");
		this.skipHidden = jEdit.getBooleanProperty("search.skipHidden.toggle");
		List<String> files = new ArrayList<String>();
		for (final String directory: dirs) {
			files.addAll(Arrays.asList(getForOneDir(directory, comp)));
		}
		String[] fileArray = new String[files.size()];
		int i = 0;
		for (String f: files) {
			fileArray[i] = f;
			i++;
		}
		return fileArray;
	} //}}}

	//{{{ Private members
	private List<String> dirs;
	private String glob;
	private boolean recurse;
	private boolean skipHidden;
	private boolean skipBinary;
	//}}}
}
